# Contributor: Bart Ribbers <bribbers@disroot.org>
# Contributor: Antoni Aloy <aaloytorrens@gmail.com>
# Maintainer: Bart Ribbers <bribbers@disroot.org>
pkgname=supertuxkart
pkgver=1.3
pkgrel=1
arch="all"
url="https://supertuxkart.net/"
pkgdesc="Kart racing game featuring Tux and his friends"
license="GPL-3.0-or-later"
depends="$pkgname-data"
makedepends="
	bluez-dev
	cmake
	curl-dev
	freetype-dev
	fribidi-dev
	glew-dev
	harfbuzz-dev
	jpeg-dev
	libpng-dev
	libvorbis-dev
	libxkbcommon-dev
	libxrandr-dev
	openal-soft-dev
	sdl2-dev
	sqlite-dev
	wayland-dev
	"
source="https://github.com/supertuxkart/stk-code/releases/download/$pkgver/SuperTuxKart-$pkgver-src.tar.xz
	server_config.xml
	"
subpackages="$pkgname-server $pkgname-static $pkgname-data::noarch"
builddir="$srcdir/SuperTuxKart-$pkgver-src"

[ "$CARCH" = "riscv64" ] && options="textrels" # Temporarily allow textrels

build() {
	case "$CARCH" in
		arm*|aarch64) GLES2_RENDERER=ON ;;
		* ) GLES2_RENDERER=OFF ;;
	esac

	# Game
	cmake -B build \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_RECORDER=0 \
		-DUSE_GLES2=$GLES2_RENDERER
	cmake --build build

	# Server only
	cmake -B build-server \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DBUILD_RECORDER=0 \
		-DSERVER_ONLY=ON
	cmake --build build-server
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# Not sure why this is in here
	rm "$pkgdir"/usr/include/wiiuse.h
}

server() {
	depends="$pkgname-data"
	cd "$builddir"
	DESTDIR="$subpkgdir" cmake --install build-server
	mv "$subpkgdir"/usr/bin/supertuxkart \
		"$subpkgdir"/usr/bin/supertuxkart-server

	# Remove desktop app, icons and pixmaps
	# Data is included in data() subpackage
	rm -r "${subpkgdir:?}"/usr/share/

	install -Dm644 "$srcdir"/server_config.xml \
		"$subpkgdir"/usr/share/supertuxkart/data/server_config.xml
}

data() {
	pkgdesc="$pkgdesc (data files)"

	amove usr/share/supertuxkart/data
}

sha512sums="
42664390c844ebca0be9b2f2aeb3c9197c0c79a7c604707b32e81ef41aec10fa36f5950f06e1540ea6f75f6c7f8a8f7f80aeda48de3d101d991420f5b20530ed  SuperTuxKart-1.3-src.tar.xz
104a0b6e6c7abca53b76895d8889edde7745e420e1579c46c63fbfba87c12080f248a1871a92e268f0010ec05ea3d7756072da49784a66156403050f6ec0056e  server_config.xml
"
